package roadtripplanner1;

import java.io.IOException;
import javax.jdo.PersistenceManager;
import javax.jdo.Query;
import javax.servlet.http.*;
import roadtripplanner1.Waypoints;
import roadtripplanner1.PMF;
import java.io.*;
import java.util.Date;
import java.util.List;
import java.util.logging.Logger;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.text.DateFormat;


public class getWaypointsServlet extends HttpServlet 
{
	private static final Logger log = Logger.getLogger(getWaypointsServlet.class.getName());
	public void doPost(HttpServletRequest req, HttpServletResponse resp)
	throws IOException 
	{
		String str = "";
		log.info("start");
		String user    = req.getParameter("theuser");
		log.info("user ="+user);
		String date    = req.getParameter("thedate");
		DateFormat formatter = new SimpleDateFormat("d MMM yyyy HH:mm:ss:SSS");
		Date formattedDate = null;

		if (!date.equals("0")) // convert to date format only if it's a real date
		{
			try 
			{
				formattedDate = formatter.parse(date);
			} 
			catch (ParseException e) 
			{
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		PersistenceManager pm = PMF.get().getPersistenceManager();
		
		//String query = "select from " + Waypoints.class.getName()
		//			   +" where user == userParam"
		//			   +" parameters String userParam"
		//			   +" order by date desc";
		
		Query query = pm.newQuery(Waypoints.class);
		query.setFilter("user == userParam");
		query.setOrdering("date desc");
		query.declareImports("import java.util.Date");
		query.declareParameters("String userParam, Date dateParam");
		
		// 2 modes of query: 1. without date- to receive all available saves
		//                   2. with date- to receive the specific saved data

		if (date.equals("0"))		
		// mode 1
		{
			List<Waypoints> waypoints = (List<Waypoints>) query.execute(user, formattedDate);
			int i = 1;
			
			// go over max 20 elements in list and retrieve the dates into a string representation
			for (Waypoints point : waypoints)
			{
				// return max 20 latest saves
				if (21 == i)
				{
					break;
				}
				
				if (1 == i)
				{
					str += formatter.format(point.getDate());
				}
				else
				{
					str += "#" + formatter.format(point.getDate());
				}
				i++;
			}
			
			PrintWriter out = resp.getWriter();
			out.println(str);
		}
		else
		// mode 2
		{
			// include the date in the search criteria
			query.setFilter("date == dateParam");
			List<Waypoints> waypoints = (List<Waypoints>) query.execute(user, formattedDate);
			// get first (newest) element of the results if any were returned
			if (waypoints.size() > 0)
				str = waypoints.get(0).getData();
			//str = "string date:"+ date + " Date date:" + formattedDate+" length of res: "+ waypoints.size()+ " date of result:"+ waypoints.get(0).getDate();
			PrintWriter out = resp.getWriter();
			out.println(str);
		}
	}
}
		